package znbkTopicType.content;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperCtfd.activity.WrongTopicTutoringPaperActivity.mEntityCtfd;
import static znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity.mEntityNtxl;
import static znbkTestPaperRdqh.activity.WeekAscensionPaperActivity.mEntityRdqh;
import static znbkTestPaperXtfx.activity.SystemReviewPaperActivity.mEntityXtfx;

/**
 * Created by Administrator on 2018\2\23 0023.
 */

public class ZnbkContentXztAdapter extends WrapBaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int ItemCount;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源

    private ViewHolder holder = null;
    public class ViewHolder{
        WebView WvTheme;
        public ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
        }
    }

    private String QuesBody;//获取大题题干信息（阅读理解，完形选择）
    private String QuesBrief;//(语言表达)
    private int totalItemCount;//选择题个数
    private String typeName;//题目类型
    private double correctRate;//准确率
    private  int status;//作答状态
    private double score;//小题总分


    public ZnbkContentXztAdapter(Context context, int itemCount, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.ItemCount = itemCount;
        this.PaperID = paperID;
        this.ModelFrom =modelFrom;
    }

    @Override
    public int getCount() {
        return ItemCount;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_xzt_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取试题相关信息
        * */
        getContentInfor();

        /*
        * 显示大题题干
        * */
        showContentInfor();

        return convertView;
    }

    /*
    * 获取试题信息
    * */
    private void getContentInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS) {
            status = mEntityNtxl.getAnswerState();
            typeName = mEntityNtxl.getTKQue().getTypeName();
            totalItemCount = mEntityNtxl.getTKQue().getTotalItemCount();
            QuesBody = mEntityNtxl.getTKQue().getQuesBody();
            QuesBrief = mEntityNtxl.getTKQue().getQuesBrief();
            correctRate = mEntityNtxl.getCorrectRate();
        }else if(ModelFrom ==ZnbkPaperDef.FROM_SYSTEM_REVIEW){
            status = mEntityXtfx.getModelSinglePaperInfo().getModelPaperInfo().getAnswerState();
            score = mEntityXtfx.getModelSinglePaperInfo().getModelPaperInfo().getDetail().get(PaperID).getScore();
            typeName = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getTypeName();
            totalItemCount = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getTotalItemCount();
            QuesBody = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getQuesBody();
            QuesBrief = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getQuesBrief();
        }else if(ModelFrom ==ZnbkPaperDef.FROM_WEEK_ASCENSION){
            status = mEntityRdqh.getModelSinglePaperInfo().getModelPaperInfo().getAnswerState();
            score = mEntityRdqh.getModelSinglePaperInfo().getModelPaperInfo().getDetail().get(PaperID).getScore();
            typeName = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getTypeName();
            totalItemCount = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getTotalItemCount();
            QuesBody = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getQuesBody();
            QuesBrief = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(PaperID).getQuesBrief();
        }else if(ModelFrom == ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
            status = mEntityCtfd.getAnswerState();
            int anotherPaperID = -1;
            if(PaperID==0){
                typeName = mEntityCtfd.getPrimitiveQues().getTKQue().getTypeName();
                totalItemCount = mEntityCtfd.getPrimitiveQues().getTKQue().getTotalItemCount();
                QuesBody = mEntityCtfd.getPrimitiveQues().getTKQue().getQuesBody();
                QuesBrief = mEntityCtfd.getPrimitiveQues().getTKQue().getQuesBrief();
            }else if(PaperID==1){
                anotherPaperID = PaperID-1;
                if(mEntityCtfd.getClassicalQues()!=null){
                    typeName = mEntityCtfd.getClassicalQues().getTKQue().getTypeName();
                    totalItemCount = mEntityCtfd.getClassicalQues().getTKQue().getTotalItemCount();
                    QuesBody = mEntityCtfd.getClassicalQues().getTKQue().getQuesBody();
                    QuesBrief = mEntityCtfd.getClassicalQues().getTKQue().getQuesBrief();
                }else{
                    typeName = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getTypeName();
                    totalItemCount = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getTotalItemCount();
                    QuesBody = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBody();
                    QuesBrief = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBrief();
                }
            }else{
                if(mEntityCtfd.getClassicalQues()!=null){
                    anotherPaperID = PaperID-2;
                }else{
                    anotherPaperID = PaperID-1;
                }
                typeName = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getTypeName();
                totalItemCount = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getTotalItemCount();
                QuesBody = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBody();
                QuesBrief = mEntityCtfd.getRecQues().get(anotherPaperID).getTKQue().getQuesBrief();
            }
        }

        if(typeName.equals("完形选择")){
            String arrayTheme[] = new String[totalItemCount + 1];
            String themeAll = "";
            for (int i = 0; i < totalItemCount + 1; i++) {
                arrayTheme[i] = Control.returnArray(QuesBody, "____", 4)[i];
                if (i < totalItemCount) {
                    themeAll = themeAll + arrayTheme[i] + "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                } else {
                    themeAll = themeAll + arrayTheme[i];
                }
            }
            QuesBody = StringEscapeUtils.unescapeXml(themeAll);
        }else if(typeName.equals("语言表达")){
            QuesBody = StringEscapeUtils.unescapeXml(QuesBrief);
        }else {
            QuesBody = StringEscapeUtils.unescapeXml(QuesBody);
        }

    }


    /*
    * 获取题目题干信息
    * */
    private void showContentInfor(){
        holder.WvTheme.loadDataWithBaseURL("about:blank", QuesBody, "text/html", "utf-8", null);
        holder.WvTheme.getSettings().setDefaultFontSize(18);
        holder.WvTheme.setFocusable(false);

    }


}